package com.topscore.uke.main;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.Toast;


import com.topscore.uke.utils.SnackBarUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Li Guoxi on 2016/2/15.
 */
public class ATManager {
    private static List<Activity> activityList = new LinkedList<>();
    private static long exitTime = 0;

    public static void add(Activity activity) {
        activityList.add(activity);
    }

    public static void backExit(Context context) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(context, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            exitClient(context);
        }
    }

    public static void backExit(Context context, View view) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            SnackBarUtil.getInstance(view).showShort("再按一次退出应用").setMessageColor(Color.WHITE).show();
//            ToastUtil.toastShort(context, "再按一次退出应用");
            exitTime = System.currentTimeMillis();
        } else {
            exitClient(context);
        }
    }

    public static List<Activity> getActivityList() {
        return activityList;
    }

    public static void exitClient(Context context) {
        for (int i = 0; i < activityList.size(); i++) {
            if (activityList.get(i) != null) {
                activityList.get(i).finish();
            }
        }
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        int version = Build.VERSION.SDK_INT;
        if (version < 8) {
            activityManager.restartPackage("com.topscore.uke");
        } else {
            activityManager.killBackgroundProcesses("com.topscore.uke");
        }
        System.exit(0);
    }


}
